home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
oper_sys
/
oasis
/
oasisegs.lha
/
egs
/
tsp.lisp
< prev
next >
Wrap
Lisp/Scheme
|
1992-04-17
|
2KB
|
68 lines
(proclaim '(type (array fixnum 2) *dist*))
(proclaim '(type list *best*))
(proclaim '(type fixnum *min*))
(proclaim '(function run (fixnum) fixnum))
(proclaim '(function explore (list list list fixnum) fixnum))
(proclaim '(function expand (list list list fixnum) fixnum))
(proclaim '(function update (list fixnum) nil))
(proclaim '(function gen (fixnum) list))
(defvar *dist* nil)
(defvar *best* nil)
(defvar *min* 65536)
(defun run (n)
(declare (type fixnum n))
(setf *dist* (make-array (cons n (cons n nil))
:element-type 'fixnum
:initial-element 0))
(setf *best* nil)
(setf *min* 65536)
(explore (cdr (gen n)) '() '(0) 0) )
(defun explore (c1 c2 tour sum)
(declare (type list c1)
(type list c2)
(type list tour)
(type fixnum sum))
(cond ((<= *min* sum) *min*)
((and (null c1) (null c2)) (update tour (+ sum (aref *dist* (car tour) 0))))
((expand c1 c2 tour sum) (expand c2 c1 tour sum)) ))
(defun expand (cities c2 tour sum)
(declare (type list cities)
(type list c2)
(type list tour)
(type fixnum sum))
(if (null cities) *min*
(let ((x (car cities))
(y (car tour))
(c1 (cdr cities)) )
(explore c1 c2 (cons x (cons y (cdr tour))) (+ sum (aref *dist* x y)))
(expand c1 (cons x c2) tour sum) )))
(defun update (tour sum)
(declare (type list tour)
(type fixnum sum))
(when (> *min* sum) (setf *best* tour) (setf *min* sum)) )
(defun gen (n)
(declare (type fixnum n))
(let ((seed 197)
(b 0)
(cities nil) )
(declare (type fixnum seed)
(type fixnum b)
(type list cities))
(do ((i 0 (+ i 1)))
((= i n) (reverse cities))
(declare (type fixnum i))
(do ((j (+ i 1) (+ j 1)))
((= j n) (setf cities (cons i cities)))
(declare (type fixnum j))
(setf seed (rem (+ (* 4757 seed) 1) 32768))
(setf b (+ 1 (rem (truncate (/ seed 16)) 256)))
(setf (aref *dist* i j) b)
(setf (aref *dist* j i) b) ))))